<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise的基础概念</title>
</head>
<body>
    <h1>Promise的基础概念</h1>
    <h2>Promise 是异步编程的一种解决方案</h2>

    <script>
        // Promise基本用法
        const promise = new Promise(function(resolve, reject) {
            // 异步操作
            setTimeout(() => {
                if(Math.random() > 0.5) {
                    resolve('成功');
                } else {
                    reject('失败');
                }
            }, 1000);
        });

        promise.then(
            value => console.log('成功：', value),
            error => console.log('失败：', error)
        );

        // Promise的状态
        const p1 = new Promise((resolve, reject) => {
            resolve('fulfilled'); // Promise变为fulfilled状态
        });

        const p2 = new Promise((resolve, reject) => {
            reject('rejected'); // Promise变为rejected状态
        });

        // Promise的链式调用
        new Promise((resolve, reject) => {
            resolve(1);
        }).then(value => {
            console.log(value); // 1
            return value * 2;
        }).then(value => {
            console.log(value); // 2
            return value * 2;
        }).then(value => {
            console.log(value); // 4
        });
    </script>
</body>
</html> 